import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';

export default {
  name: 'pointto_teacher',
  components: {},
  data () {
    return {
      siteId: '',
      studentId: '',
      spelName: '',
      area: '',
      term: '',
      weekDay: '',
      specialtyText:'专业',
      spelId: '',
      pageNo: 1,
      content: [],
      optionsArea: [],
      propsArea: {
        value: 'value',
        children: 'cities'
      },
      parentCodeArea: '0',
      optionsTerm: [],
      optionsSpelId: [],
      optionsWeekDay: [],
      selectedOptionsArea: [],
      selectedOptionsTerm: [],
      selectedOptionsSpelId: [],
      selectedOptionsWeekDay: []
    }
  },
  mounted () {
    this.oauth()
    let areaId = window.sessionStorage.getItem('areaId')
    let areaName = window.sessionStorage.getItem('areaName')
    this.area = areaId
    this.spelName = areaName
    this.siteId = window.sessionStorage.getItem('siteId')
    if (this.siteId == '71F7B4B5ADF92308E0530100007F712B') {
      this.specialtyText = '学校';
    }
    this.studentId = window.sessionStorage.getItem('studentId')
    if (this.studentId == '-1') {
      this.studentId = ''
    }
    this.arealist()
    this.changeArea()
    this.termlist()
    this.weeklist()
  },
  methods: {
    oauth() {
      //var s = 'http://txzfzx.hztangong.com/zfzx/wx/index.html?siteId=FF8080813369F27801338627E88E100A&wxAvatar=http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLzWQgqxY3kMPxyWnNric9AgX4iayCB7TnRfibQs4sQ37RnU4m85vo5EbAWPDrB07XVSRltXibVOdibDBQ/132&appId=wx748e40bf9deaa2d0&payOpen=1&studentId=27C53C7D71FB7DE0E0530100007F6947&studentName=%E9%BB%84%E7%8E%89%E6%9E%97&openId=oXZJV0sllFw5QfHXknvBBMMxZx5M#/area';
      //var s = 'http://txzfzx.hztangong.com/zfzx/wx/index.html?siteId=8A4081E62DCAB2D8012E6B1596B40EE7&wxAvatar=http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLzWQgqxY3kMPxyWnNric9AgX4iayCB7TnRfibQs4sQ37RnU4m85vo5EbAWPDrB07XVSRltXibVOdibDBQ/132&appId=wx748e40bf9deaa2d0&payOpen=1&studentId=27C53C7D71FB7DE0E0530100007F6947&studentName=%E9%BB%84%E7%8E%89%E6%9E%97&openId=oXZJV0sllFw5QfHXknvBBMMxZx5M#/area';
      //var s = 'http://txzfzx.hztangong.com/zfzx/wx/index.html?siteId=2C9146A12760F52A01276124EFB90016&wxAvatar=http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLzWQgqxY3kMPxyWnNric9AgX4iayCB7TnRfibQs4sQ37RnU4m85vo5EbAWPDrB07XVSRltXibVOdibDBQ/132&appId=wxaa5ef9f176491685&payOpen=1&studentId=27C53C7D71FB7DE0E0530100007F6947&studentName=%E9%BB%84%E7%8E%89%E6%9E%97&openId=oXZJV0sllFw5QfHXknvBBMMxZx5M#/area';
      var openId = window.sessionStorage.getItem('openId')
      if (openId == null || openId == '' || openId == 'undefined') {
        var s = window.location.href
        var arr = s.split('?')
        var url = arr[1]
        var theRequest = new Object()
        var strs = url.split('&')
        for (var i = 0; i < strs.length; i++) {
          //alert(strs[i])
          var content = strs[i].split('=')
          theRequest[content[0]] = content[1]
        }
        if (theRequest.openId == null) {
          location.href = "http://bm.qsng.cn/eduplatform/html/api/public/wx/wxOAuth?appId=" + theRequest.appId
        } else {
          var openIds = theRequest.openId.split('#/')
          window.sessionStorage.setItem('openId', openIds[0])
          window.sessionStorage.setItem('studentId', theRequest.studentId)
          window.sessionStorage.setItem('studentName', decodeURIComponent(theRequest.studentName))
          window.sessionStorage.setItem('siteId', theRequest.siteId)
          window.sessionStorage.setItem('userType', theRequest.userType)
          window.sessionStorage.setItem('wxAvatar', theRequest.wxAvatar)
          window.sessionStorage.setItem('appId', theRequest.appId)
          window.sessionStorage.setItem('payOpen', theRequest.payOpen)
          var currentUrl = window.location.href
          var targetUrl = currentUrl.replace(/openId=/, '')
          console.log(targetUrl.replace(/studentId=/, ''))
          window.location.href = targetUrl.replace(/studentId=/, '')
          //history.pushState(null,null,currentUrl)
          //alert(window.location.href)
        }
      }
    },
    changeAreaSelected (val) {
      var _this = this
      _this.area = val[0]
      _this.classList()
      for (var n = 0; n < _this.optionsSpelId.length; n++) {
        if (val[0] == _this.optionsSpelId[n].value) {
          _this.spelName = _this.optionsSpelId[n].label
          return
        }
      }
    },
    changeTermSelected (val) {
      var _this = this
      _this.term = val[0]
      _this.classList()
    },
    changeSpelIdSelected (val) {
      var _this = this
      _this.spelId = val[1]
      _this.classList()
    },
    changeWeekDaySelected (val) {
      var _this = this
      _this.weekDay = val[0]
      _this.classList()
    },
    lookmore () {
      var _this = this
      if (_this.pageNo >= 1) {
        _this.pageNo++
        this.classList()
      }
    },
    classList () {
      var _this = this
      //console.log(_this.siteId)

      let params = '?siteId=' + _this.siteId + '&area=' + _this.area + '&term=' + _this.term
        + '&spelId=' + _this.spelId + '&weekDay=' + _this.weekDay  + '&pageNo=' + _this.pageNo
        + '&userId=' + _this.studentId + '&userType=1';
      http.get(CONSTANT.BSPCLASSLIST + params, {
        /* siteId: _this.siteId,
         area: _this.area,
         term: _this.term,
         spelId: _this.spelId,
         weekDay: _this.weekDay,
         grade: '',
         pageNo: _this.pageNo,
         studentId: _this.studentId,
         keyWords: ''*/
      }, (res) => {
        console.log('classList', res)
      if (typeof res.content == 'object') {
        if (res.total <= 0 || res.content.length <= 0) {
          if (_this.pageNo > 1) {
            _this.pageNo--
          }
          alert('没有更多了！')
          return
        }
        if (_this.pageNo == 1) {
          _this.content = res.content
        } else if (_this.pageNo > 1) {
          _this.content = _this.content.concat(res.content)
        }
      } else {
        _this.content = []
      }
    })
    },
    handleItemChangeArea (val) {
      var _this = this
      // 地区选择 动态获取
      if (_this.parentCodeArea != val[0]) {
        _this.parentCodeArea = val[0]
        _this.changeArea()
      }
    },
    changeArea () {
      // 区域 改成了 专业
      var _this = this
      http.post(CONSTANT.SPECIALTYLIST, {orgId: _this.siteId}, (res) => {
        res.data.forEach(item => {
        if (!item.parentId && item.status == '1') {
        var areaFirst = {}
        areaFirst.label = item.name
        areaFirst.value = item.id
        areaFirst.cities = []
        _this.optionsArea.push(areaFirst)
      }
    })
      res.data.forEach(item => {
        if (item.parentId) {
        _this.optionsArea.forEach(pItem => {
          if (item.parentId == pItem.value) {
          var areaSecond = {}
          areaSecond.label = item.name
          areaSecond.value = item.id
          pItem.cities.push(areaSecond)
        }
      })
      }
      })
      })
    },
    termlist () {
      // 学期
      var _this = this
      http.post(CONSTANT.ORGDICTLIST, {orgId: _this.siteId,type: 'edu_term'}, (res) => {
        res.data.forEach(item => {
          var termFirst = {}
          termFirst.label = item.label
          termFirst.value = item.value
          _this.optionsTerm.push(termFirst)
        })
      })
    },
    arealist () {
      // 专业 改成了  区域
      var _this = this
      let params = CONSTANT.ORGDICTLIST + '?siteId=' + _this.siteId
      http.post(params, {orgId: _this.siteId,type: 'edu_business_rea'}, (res_) => {
        //console.log('quyu', res)
        let res = res_.data
      _this.spelName = res[0].label
      _this.area = res[0].value
      _this.selectedOptionsSpelId[0] = res[0].resCode
      _this.classList()
      for (var s = 0; s < res.length; s++) {
        var termFirst = {}
        termFirst.label = res[s].label
        termFirst.value = res[s].value
        _this.optionsSpelId.push(termFirst)
      }
    })
    },
    weeklist () {
      // 时间段
      var _this = this
      http.post(CONSTANT.DICTLIST, {orgId: _this.siteId,type: 'sys_weekday'}, (res) => {
        res.data.forEach(item => {
          var weekFirst = {}
          weekFirst.label = item.label
          weekFirst.value = item.value
          _this.optionsWeekDay.push(weekFirst)
        })
    })
    },
    goScheduleT (id) {
      window.sessionStorage.setItem('event_id', id)//courseDetails
      this.$router.push({path: '/schedule_t'})
    }
  }
}
